//
//  LSYNetSourceController.m
//  LSYReader
//
//  Created by sam on 2018/3/31.
//  Copyright © 2018年 okwei. All rights reserved.
//
#import "LSYParseModel.h"

#import "UIView+EV.h"
#import "LSYNetSourceCell.h"
#import "LSYNetSourceController.h"

@interface LSYNetSourceController ()
@property (nonatomic,strong) UIBarButtonItem *leftButtonItem;
@end

@implementation LSYNetSourceController

+ (instancetype)netSourceController:(NSMutableArray *)sources {
    LSYNetSourceController *sourceController = [[LSYNetSourceController alloc] initWithNibName:@"LSYNetSourceController" bundle:nil];
    sourceController.sources = sources;
    return sourceController;
}

- (void)reloadData {
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"LSYNetSourceCell" bundle:nil] forCellReuseIdentifier:@"LSYNetSourceCell"];
    [self.tableView reloadData];
    self.navigationItem.leftBarButtonItem = self.leftButtonItem;
    self.title = @"书源";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIBarButtonItem *)leftButtonItem {
    if (_leftButtonItem) {
        return _leftButtonItem;
    }
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0, 0, 44, 44)];
    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [leftButton setImage:[UIImage imageNamed:@"ic_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(tapLeftBarButton) forControlEvents:UIControlEventTouchUpInside];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    leftView.bounds = CGRectOffset(leftView.bounds, 10, 0);
    [leftView addSubview:leftButton];
    _leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    return _leftButtonItem;
}

#pragma mark - tap left bar button
- (void)tapLeftBarButton {
    [UIView animateWithDuration:0.3 animations:^{
        self.navigationController.view.ev_left = [UIScreen mainScreen].bounds.size.width;
    } completion:^(BOOL finished) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSYNetSourceCell *cell = (LSYNetSourceCell *)[tableView dequeueReusableCellWithIdentifier:@"LSYNetSourceCell" forIndexPath:indexPath];
    LSYParseModel *model = self.sources[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LSYParseModel *model = self.sources[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedRule:)]) {
        [self.delegate selectedRule:model];
    }
}
@end
